﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace MySQLApplication
{
    class MySQLHandler
    {
        string str_connectionString;

        public static MySqlConnection connection = null;
        public static MySqlCommand cmd = new MySqlCommand();
        public MySQLHandler()
        {
            getCredentials();
        }

        public void getCredentials()
        {
            Console.Write("IP Address (nothing for default 'localhost'):  ");
            string str_IP = Console.ReadLine();

            if (str_IP.Equals("")) // If they hit enter without typing anything
                str_IP = "127.0.0.1"; // Set the IP to local IP.

            // Root username by default on mySQL servers is 'root'
            Console.Write("Username (nothing for default 'root'):  ");
            string str_username;
            str_username = Console.ReadLine();

            if(str_username.Equals("")) // If they hit enter without typing anything
                str_username = "root"; // Set the username to "root"

            // It's really silly if they have their mysql password set to 'root', but this is mostly just for testing easily so I can hit enter 4 times to connect to my local mysql server
            Console.Write("Password (nothing for default 'root'):  ");
            string str_password = "";

            if(str_password.Equals(""))
                str_password = "root";

            // This is the fancy "security" measure, just to make sure anyone peeking over your shoulder or viewing your screen can not see your password you're typing in.
            // In an ideal world, I would have made this encrypted 3 tiems over and unviewable to keyloggers, but alas, ain't nobody got time for that.  That will be done at a later date
            // When I have enough time to learn an all new concept like data encryption.
            while (true)
            {
                ConsoleKeyInfo i = Console.ReadKey(true);
                if (i.Key == ConsoleKey.Enter)
                {
                    break;
                }
                else if (i.Key == ConsoleKey.Backspace)
                {
                    if (str_password.Length > 0)
                    {
                        str_password = str_password.Remove(str_password.Length - 1);
                    }
                }
                else
                {
                    str_password += i.KeyChar;
                    Console.Write("");
                }
            }
            Console.WriteLine();
            try  // Basically to make sure they actually connect to a server.
            {
                connect(str_IP, str_username, str_password);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                getCredentials();
            }
        }

        public int connect(string IP, string username, string password)
        {
            string str_database;
            Console.WriteLine("What database are we using?");
            str_database = Console.ReadLine();
            str_connectionString = @"server=" + IP + ";userid=" + username + ";password=" + password + ";database=" + str_database;

            try
            {
                connection = new MySqlConnection(str_connectionString);
                connection.Open();
                cmd.Connection = connection;
                Console.WriteLine("Connected successfuly! MySQL Version:  {0}", connection.ServerVersion);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                getCredentials(); // Unfortunately there's no decent way to differentiate authentication errors and database-does-not-exist errors.
                // Silly, but we have to restart the whole "get credentials process"
            }
            return 0;
        }
    }
}
